<?php

/**
 * ECSHOP 商品页
 * ============================================================================
 * * 版权所有 2005-2012 上海商派网络科技有限公司，并保留所有权利。
 * 网站地址: http://www.ecshop.com；
 * ----------------------------------------------------------------------------
 * 这不是一个自由软件！您只能在不用于商业目的的前提下对程序代码进行修改和
 * 使用；不允许对程序代码以任何形式任何目的的再发布。
 * ============================================================================
 * $Author: testyang $
 * $Id: goods.php 15013 2008-10-23 09:31:42Z testyang $
*/

define('IN_ECS', true);
define('ECS_ADMIN', true);

require(dirname(__FILE__) . '/includes/init.php');

$act = !empty($_REQUEST['act']) ? $_REQUEST['act'] : 'default';
if($act == 'default'){
		$activity_id = !empty($_GET['activity_id']) ? intval($_GET['activity_id']) : 0;

		if(empty($activity_id)){

	    }

		$smarty->assign('activity_id',$activity_id);
		/*获取参加该活动的总人数*/
		$member = "SELECT COUNT(*) FROM " . $GLOBALS['ecs']->table('signinfo') . "WHERE activity_id = '$activity_id' ";
		$number = $db->getOne($member);
		$smarty->assign('number', $number);

		/*获取报名须知文章*/
		$article = "SELECT * FROM " . $GLOBALS['ecs']->table('article') . " WHERE article_id = '6' ";
		$rule=$GLOBALS['db']->getROW($article);
		$smarty->assign('sign_rule',$rule);


		/*y- 以ID查询活动详情 k*/
		$sql = "SELECT * FROM " .  $GLOBALS['ecs']->table('activity') . "where activity_id  = '$activity_id'";

		$result = $db->getROW($sql);
		/*通过activity_id在gallery表中获取活动相册*/
		$sql2 = "SELECT img_url FROM " . $GLOBALS['ecs']->table('activity_gallery') . "WHERE act_id = '$activity_id' ";
		$gallery=$db->getAll($sql2);
		// $smarty->assign('gallery',$gallery);
		// echo "<pre>";
		// print_r($gallery);
		// echo "</pre>";
		// die();
		$imgs = array();
		if(!empty($gallery)){

				foreach($gallery as $k=>$value){
					// $gallery[$k]['img_url']=$value['img_url'];
					$imgs[]=$value['img_url'];

				}

				$smarty->assign('gallery',$imgs);
		}
		// print_r($imgs);
		// 			die();
		if(!empty($result)){

			/*活动状态转换*/
			/*当前时间获取*/
			$showtime=date("Y-m-d H:i");
			$NowTime = !empty($showtime) ? local_strtotime($showtime) : 0;

			if($NowTime < $result['startsign_time']){

				$result['activity_status'] = '1';
				$result['activity_status_name'] = '预热中';
				$result['time'] = local_date('Y-m-d H:i:s', $result['startsign_time']);


			}elseif ($result['endsign_time'] > $NowTime && $NowTime > $result['startsign_time']) {

				$result['activity_status'] = '2';
				$result['activity_status_name'] = '报名中';
				// $result['time'] = $result['endsign_time'];
				$result['time'] = local_date('Y-m-d H:i:s', $result['endsign_time']);
				// echo $result['time'];
				// print_r($result['time']);
				// die("55");

			}elseif ($result['activity_stime'] > $NowTime && $NowTime > $result['startsign_time']) {	

				$result['activity_status'] = '4';
				$result['activity_status_name'] = '报名结束';
				$result['time'] = local_date('Y-m-d H:i:s', $result['activity_stime']);


			}elseif ($result['activity_etime'] > $NowTime && $NowTime >$result['activity_stime'] ) {

				$result['activity_status'] = '3';
				$result['activity_status_name'] = '活动中';
				// $result['time'] = $result['activity_etime'];
				$result['time'] = local_date('Y-m-d H:i:s', $result['activity_etime']);

			}elseif ($NowTime > $result['activity_etime']) {	

				$result['activity_status'] = '5';
				$result[$k]['activity_status_name'] = '活动结束';
				$result['time'] = local_date('Y-m-d H:i:s', '$NowTime');


			}else{	

				$result['activity_status'] = '0';
				$result['activity_status_name'] = '敬请期待';

			}
			
			/*收费状态转换*/
			switch ($result['pay_status']) {
				case '1':
					$result['pay_status'] = '收费';
					break;
				case '2':
					$result['pay_status'] = '免费';
					break;
				default:
					$result['pay_status'] = '发布方未定';
					break;		
				}	
			/*时间格式化*/	
			$result['activity_stime'] = local_date('Y-m-d', $result['activity_stime']);

			$result['activity_etime'] = local_date('Y-m-d', $result['activity_etime']);
			
			$result['endsign_time'] = local_date('Y-m-d H:i', $result['endsign_time']);
			
		}

			/* 更新预览量 */
			
			$rs=mysql_query("UPDATE wx_activity SET number = number +1 WHERE activity_id = '".$_GET['activity_id']."'");

			if(mysql_affected_rows()==1){

				$rs=mysql_query("select * from wx_activity where activity_id='".$_GET['activity_id']."'");

				$row=mysql_fetch_array($rs);
			
				// echo $row['number'];
			}

		$smarty->assign('name', $result['activity_status_name']);
		// $smarty->assign('time', $result['time']);
		$smarty->assign('djs', $result['time']);
		// echo $result['time'];
		// die();
		$smarty->assign('xqing', $result);

		/*y- 以ID查询活动详情 j*/

		$smarty->display('goods.dwt'); 
		// print_r($_REQUEST);
}


/*插入报名信息*/
if($act == 'insert_info'){

	/*获取报名信息*/
	$arr = $_POST['name'];  
	$activity_id = $_POST['activity_id'];  
	$sql = " SELECT COUNT(*) FROM " . $ecs->table('signinfo') . " WHERE  activity_id = '$_REQUEST[activity_id]' AND sign_phone = '$_REQUEST[phone]' ";
	if($db->getOne($sql)>0){
		$back_info="1";
	}
	else{
			$showtime=date("Y-m-d H:i:s");
			$NowTime = !empty($showtime) ? local_strtotime($showtime) : 0;
			$sql = "INSERT INTO " . $GLOBALS['ecs']->table('signinfo') . " (sign_name, sign_phone, sign_sex, sign_work, activity_id, sign_time) ". "VALUES ('$_REQUEST[name]', '$_REQUEST[phone]', '$_REQUEST[sex]', '$_REQUEST[work]', '$_REQUEST[activity_id]', '$NowTime' ) ";
			$success = $GLOBALS['db']->query($sql); 
			if($success){
				$back_info="4";
			}
	}

	die(json_encode($back_info));
}

?>